package com.yuyang.wifi.activitys.web;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yuyang.wifi.utils.ToastUtils;


public class HarlanWebChromeClient extends WebChromeClient {
    private TextView textview = null;
    private ProgressBar progreesbar = null;
    private ProgressDialog progressDialog = null;
    private Context context = null;
    private Activity activity;


    public HarlanWebChromeClient(Context context) {
        this.activity = (Activity) context;
        this.context = context;
    }

    public HarlanWebChromeClient(Context context, TextView titleview) {
        this.activity = (Activity) context;
        this.context = context;
        this.textview = titleview;
    }

    public HarlanWebChromeClient(Context context, ProgressBar pb) {
        this.activity = (Activity) context;
        this.context = context;
        this.progreesbar = pb;
        this.progreesbar.setMax(100);
    }

    public HarlanWebChromeClient(Context context, ProgressDialog pbdialog) {
        this.activity = (Activity) context;
        this.context = context;
        progressDialog = pbdialog;
        progressDialog.show();
    }


    /**
     * @param context
     * @param titleview 网页title显示
     * @param pb        进度条
     */
    public HarlanWebChromeClient(Context context, TextView titleview,
                                 ProgressBar pb) {
        this.activity = (Activity) context;
        this.context = context;
        this.textview = titleview;
        this.progreesbar = pb;
        this.progreesbar.setMax(100);
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        if (progreesbar != null) {
            progreesbar.setProgress(newProgress);
            if (newProgress == 100) {
                progreesbar.setVisibility(View.GONE);
            }
        } else if (progressDialog != null && progressDialog.isShowing()) {
            if (newProgress == 100) {
                progressDialog.dismiss();
            }
        }
        super.onProgressChanged(view, newProgress);
    }

    @Override
    public boolean onJsAlert(WebView view, String url, String message,
                             JsResult result) {
        ToastUtils.showShortToast(activity, message);
        result.confirm();
        return true;
    }

    /*
     * 此处覆盖的是javascript中的confirm方法。当网页需要弹出confirm窗口时，会执行onJsConfirm中的方法
     * 网页自身的confirm方法不会被调用。
     */
    @Override
    public boolean onJsPrompt(WebView view, String url, String message,
                              String defaultValue, JsPromptResult result) {
        result.confirm();
        return true;
    }

    /*
     * 如果页面被强制关闭,弹窗提示：是否确定离开？ 点击确定 保存数据离开，点击取消，停留在当前页面
     */
    @Override
    public boolean onJsBeforeUnload(WebView view, String url, String message,
                                    JsResult result) {
        result.confirm();
        return true;
    }


}